import XCTest
@testable import RxFDNetwork
@testable import RxSwift
@testable import RxCocoa
@testable import Alamofire

final class RxFDNetworkTests: XCTestCase {
    var disposeBag = DisposeBag()
    func testGetRequest() {
        let expectation = XCTestExpectation(description: "get songs from netease")
        RxFDNetwork.getSongs(key: "心愿便利贴")
            .subscribe(onNext: { model in
                XCTAssertTrue(model.result?.songs.count ?? 0 > 0, "should have at least one song")
                expectation.fulfill()
            }, onError: { error in
                debugPrint(error.localizedDescription)
                XCTFail("get songs failed: \(error.localizedDescription)")
            }).disposed(by: disposeBag)
        wait(for: [expectation], timeout: 5.0)
    }
}

extension RxFDNetwork {
    static func getSongs(key: String) -> Observable<SearchModel> {
        fdJSON(getRequestGet(url: "https://netease-cloud-music-api-five-nu.vercel.app/search?keywords=\(key)"))
    }
}

extension RxFDNetwork {
    static func getRequest(method: Alamofire.HTTPMethod = .post,
                           url: String,
                           parameters: [String: Any]? = nil) -> FDRequest {
        FDRequest(method: method, url: url, parameters: parameters, encoding: JSONEncoding.default, headers: [
            "Content-Type" : "application/json",
        ])
    }
    static func getRequestGet(url: String,
                              parameters: [String: Any]? = nil) -> FDRequest {
        FDRequest(method: .get, url: url, parameters: parameters, headers: [:
        ])
    }
}
